package com.huiztech.kaoqinji;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Date;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Layout;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.AlignmentSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.huiztech.common.util.ConnectivityUtil;
import com.huiztech.kaoqinji.mvc.KaoqinController;
import com.huiztech.kaoqinji.photo.CropImageActivity;
import com.huiztech.kaoqinji.photo.ModifyAvatarDialog;

public class AddClientActivity extends BaseActivity implements OnClickListener {

    private LinearLayout backLayout, submitLayout;
    private EditText companyNameEdit, clientNameEdit, clientJobEdit, clientPhoneEdit;
    private ImageView clientImage;
    private String TAG = "";
    public static final String IMAGE_PATH = "Kaoqinji";
    private static final int FLAG_CHOOSE_IMG = 5;
    private static final int FLAG_MODIFY_FINISH = 7;
    private static String localTempImageFileName = "";
    public static final File FILE_SDCARD = Environment.getExternalStorageDirectory();
    public static final File FILE_LOCAL = new File(FILE_SDCARD, IMAGE_PATH);
    public static final File FILE_PIC_SCREENSHOT = new File(FILE_LOCAL, "images/screenshots");
    private String path = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_client);
        findView();
        backLayout.setOnClickListener(this);
        submitLayout.setOnClickListener(this);
        clientImage.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.client_back_layout:
            finish();
            break;
        case R.id.client_image:
            photoDialog();
            break;
        case R.id.client_submit_layout:
            if (!ConnectivityUtil.isNetworkAvailable(this)) {
                Toast.makeText(this, "无网状态", 0).show();
                //TODO: storeCustomerToDB()
            } else {
                try {
                    if (companyNameEdit.getText().toString().trim().equals("")
                            || clientNameEdit.getText().toString().trim().equals("")
                            || clientJobEdit.getText().toString().trim().equals("")
                            || clientPhoneEdit.getText().toString().trim().equals("")) {
                        view.showMessage(R.string.InputCustomerDetail);
                    } else {
                        if (!path.equals("")) {
                            controller.addCustomer(companyNameEdit.getText().toString(), "", clientNameEdit.getText()
                                    .toString(), new File(path), clientJobEdit.getText().toString(), clientPhoneEdit
                                    .getText().toString(), "", "");
                        } else {
                            controller.addCustomer(companyNameEdit.getText().toString(), "", clientNameEdit.getText()
                                    .toString(), new File(""), clientJobEdit.getText().toString(), clientPhoneEdit
                                    .getText().toString(), "", "");
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            break;
        default:
            break;
        }
    }

    private void findView() {
        backLayout = (LinearLayout) findViewById(R.id.client_back_layout);
        submitLayout = (LinearLayout) findViewById(R.id.client_submit_layout);
        companyNameEdit = (EditText) findViewById(R.id.client_company_name_edit);
        clientNameEdit = (EditText) findViewById(R.id.client_name_edit);
        clientJobEdit = (EditText) findViewById(R.id.client_job_edit);
        clientPhoneEdit = (EditText) findViewById(R.id.client_phone_edit);
        clientImage = (ImageView) findViewById(R.id.client_image);
    }

    private void photoDialog() {
        // 调用选择那种方式的dialog
        ModifyAvatarDialog modifyAvatarDialog = new ModifyAvatarDialog(this) {
            // 选择本地相册
            @Override
            public void doGoToImg() {
                this.dismiss();
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, FLAG_CHOOSE_IMG);
            }

            // 选择相机拍照
            @Override
            public void doGoToPhone() {
                this.dismiss();
                String status = Environment.getExternalStorageState();
                if (status.equals(Environment.MEDIA_MOUNTED)) {
                    try {
                        localTempImageFileName = "";
                        localTempImageFileName = String.valueOf((new Date()).getTime()) + ".png";
                        File filePath = FILE_PIC_SCREENSHOT;
                        if (!filePath.exists()) {
                            filePath.mkdirs();
                        }
                        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        File f = new File(filePath, localTempImageFileName);
                        // localTempImgDir和localTempImageFileName是自己定义的名字
                        Uri u = Uri.fromFile(f);
                        intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, u);
                        startActivityForResult(intent, 1);
                    } catch (ActivityNotFoundException e) {
                        //
                    }
                }

            }
        };
        AlignmentSpan span = new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER);
        AbsoluteSizeSpan span_size = new AbsoluteSizeSpan(25, true);
        SpannableStringBuilder spannable = new SpannableStringBuilder();
        String dTitle = getString(R.string.gl_choose_title);
        spannable.append(dTitle);
        spannable.setSpan(span, 0, dTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannable.setSpan(span_size, 0, dTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        modifyAvatarDialog.setTitle(spannable);
        modifyAvatarDialog.show();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == FLAG_CHOOSE_IMG && resultCode == RESULT_OK) {
            if (data != null) {
                Uri uri = data.getData();
                if (!TextUtils.isEmpty(uri.getAuthority())) {
                    Cursor cursor = getContentResolver().query(uri, new String[] { MediaStore.Images.Media.DATA },
                            null, null, null);
                    if (null == cursor) {
                        view.showMessage(R.string.ImageNotFound);
                        return;
                    }
                    cursor.moveToFirst();
                    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                    cursor.close();
                    Log.i(TAG, "path=" + path);
                    Intent intent = new Intent(this, CropImageActivity.class);
                    intent.putExtra("path", path);
                    startActivityForResult(intent, FLAG_MODIFY_FINISH);
                } else {
                    Log.i(TAG, "path=" + uri.getPath());
                    Intent intent = new Intent(this, CropImageActivity.class);
                    intent.putExtra("path", uri.getPath());
                    startActivityForResult(intent, FLAG_MODIFY_FINISH);
                }
            }
        } else if (requestCode == 1 && resultCode == RESULT_OK) {
            File f = new File(FILE_PIC_SCREENSHOT, localTempImageFileName);
            Intent intent = new Intent(this, CropImageActivity.class);
            intent.putExtra("path", f.getAbsolutePath());
            startActivityForResult(intent, FLAG_MODIFY_FINISH);

        } else if (requestCode == FLAG_MODIFY_FINISH && resultCode == RESULT_OK) {
            if (data != null) {
                path = data.getStringExtra("path");
                Log.i(TAG, "截取到的图片路径是 = " + path);
                Bitmap b = BitmapFactory.decodeFile(path);
                clientImage.setImageBitmap(b);

            }
        }
    }
}
